package com.dongdongshop.config;
import com.dongdongshop.api.SellerServiceApi;
import com.dongdongshop.api.UserServiceApi;
import com.dongdongshop.data.Result;
import com.dongdongshop.dto.SellerDTO;
import com.dongdongshop.dto.UserDto;
import com.dongdongshop.enums.TokenEnum;
import com.dongdongshop.exception.LoginException;
import com.dongdongshop.model.TbPerson;
import com.dongdongshop.service.PersonService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class LoginRealm extends AuthorizingRealm {

    @Autowired
    private PersonService personService;
    @Autowired
    private SellerServiceApi sellerServiceApi;

    @Autowired
    private UserServiceApi usa;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addStringPermission("del");
        return info;
    }

    //登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("登录方法");
        LoginToken token = (LoginToken) authenticationToken;
        String personname = token.getUsername();
        TokenEnum tokenEnum = token.getTokenEnum();
        if (tokenEnum.equals(TokenEnum.ADMIN)) {
            TbPerson person = personService.getUserByName(personname);
            //判断用户名是否正确
            if (person == null) {
                return null;
            }
            return new SimpleAuthenticationInfo(person, person.getPwd(), ByteSource.Util.bytes(person.getSalt()), "LoginRealm");   //用户名，密码，名称
        } else if (tokenEnum.equals(TokenEnum.SELLER)) {
            Result<SellerDTO> result = sellerServiceApi.getSellerByName(personname);
            SellerDTO sellerDTO=result.getData();
            if(sellerDTO==null){
                return null;
            }
            if(!"1".equals(sellerDTO.getStatus())){
                throw new LoginException("账户状态异常");
            }
        return new SimpleAuthenticationInfo(sellerDTO, sellerDTO.getPassword(), ByteSource.Util.bytes(sellerDTO.getSalt()), "LoginRealm");   //用户名，密码，名称
        }else if (tokenEnum.equals(TokenEnum.USER)){
            Result<UserDto> result = usa.getSellerByName(personname);
            UserDto userDto = result.getData();
            if (userDto == null) {
                return null;
            }
            if (!"Y".equals(userDto.getStatus())){
                throw new LoginException("状态异常");
            }
            return new SimpleAuthenticationInfo(userDto,userDto.getPassword(),ByteSource.Util.bytes(userDto.getSalt()),"LoginRealm");

        }
        return null;
    }
}
